﻿using Ldt.DataEngine.RuleAssemble;
using LdtDataEngin.ModuleDetail.BaseModule;
using LdtDataEngin.ModuleDetail.CommonModule.LawDeduce;
using LdtJudge.DataEngine.Common.StringAssemble;
using LdtJudge.DataEngine.Common.XmlUtility;
using LdtJudge.DataEngine.DataBaseStores;
using LdtJudge.DataEngine.Model.XMLConfigModel.CircumsatanModel;
using LdtJudge.DataEngine.Model.XMLConfigModel.CourtTrailQuotLaw;
using LdtJudge.DataEngine.Model.XMLConfigModel.DataStores;
using LdtJudge.DataEngine.Model.XMLConfigModel.LawElementModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Xml.Linq;

namespace LdtDataEngin.ModuleDetail.AnalysisModule.CourtTrailQuotLawExtr
{
    /// <summary>
    /// 案件审理阶段引用法规提取
    /// </summary>
    public class CourtTrailQuotLawExtr : QuotLawBase
    {
        /// <summary>
        /// 模块名称
        /// </summary>
        public string ModuleName { get; set; }

        // 配置信息
        private CircumsatanModel courtTrailQuotLawConfig = new CircumsatanModel();

        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="context"></param>
        /// <param name="moduleName"></param>
        public CourtTrailQuotLawExtr(DataContext context,string moduleName)
        {
            base.dataContext = context;
            this.ModuleName = moduleName;
        }

        /// <summary>
        /// 法律法规提取
        /// </summary>
        /// <param name="dataModelConvicted"></param>
        public void Extract(DataModel dataModelConvicted)
        {
            ParseConfig();
            string orgText = dataModelConvicted.AttributesList.FirstOrDefault(a => a.Name == "原文").sourceList.FirstOrDefault(a => a.IsCheck == "1").SaveValue;
            string oCollReg = RuleParseClass.GerRegValue("QuoteLawExtractA1");
            MatchCollection oColl = Regex.Matches(orgText, oCollReg);
            for (int i = 0; i < oColl.Count; i++)
            {
                Match oMatch = oColl[i];
                string catname = oMatch.Groups[1].Value;
                string newCatName = catname;
                newCatName = Regex.Replace(catname, "<", "《");
                newCatName = Regex.Replace(newCatName, ">", "》");
                //查询获取相应的法规ID；
                string LawLibraryCode = string.Empty;
                //string CatID = QuoteLawUsing.QuoteLawExt.GetLawID(newCatName, out LawLibraryCode);
                ///返回法律的数据对象
                var CatIDmodel = QuoteLawExt.GetLawIDTupleModel(newCatName, out LawLibraryCode);
                string quoteProvText = oMatch.Value;
                if (CatIDmodel.Item1 == string.Empty)
                {
                    newCatName = Regex.Replace(catname, "<|>", "");
                    CatIDmodel = QuoteLawExt.GetLawIDTupleModel(newCatName, out LawLibraryCode);
                }
                if (CatIDmodel.Item1 == string.Empty)
                {
                    newCatName = Regex.Replace(catname, "<|>", "%");
                    CatIDmodel = QuoteLawExt.GetLawIDTupleModel(newCatName, out LawLibraryCode);
                }
                if (CatIDmodel.Item1 == string.Empty)
                {
                    continue;
                }
                string quoteLawID = string.Empty;
                string quoteID = string.Empty;
                ///法律的ID
                string CatID = CatIDmodel.Item1;
                LawModel lawModel = AddLaw(CatID, CatIDmodel.Item2);
                string quoteProvItemCollReg = RuleParseClass.GerRegValue("QuoteLawExtractA3");
                //匹配法律条款;
                string tempReg = RuleParseClass.GerRegValue("QuoteLawExtractA4");
                //将替换后的文本进行重新添加和拼接返回;正则操流程;
                quoteProvText = RuleParseClass.GetParseValue("QuoteLawExtractA9", quoteProvText);
                MatchCollection quoteProvItemColl = Regex.Matches(quoteProvText, quoteProvItemCollReg);
                for (int j = 0; j < quoteProvItemColl.Count; j++)
                {
                    //循环添加引用发条
                    Match provmatch = quoteProvItemColl[j];
                    string singleQuoteprov = provmatch.Value;
                    Match tempMatch = Regex.Match(singleQuoteprov, tempReg);
                    string provName = (tempMatch == null) ? "" : tempMatch.Value;
                    string provChineseNumb = (tempMatch == null) ? "" : tempMatch.Groups[1].Value;
                    Match tempMatchtiao = Regex.Match(provName, "第.+?条之([一二三四五六七八九十零]+|[0-9]+)");
                    int provNumbzhi = Convert.ToInt32(LdFormat.DecodeNumberCn(tempMatchtiao.Groups[1].Value));
                    //条文序号
                    int provNumb = Convert.ToInt32(LdFormat.DecodeNumberCn(provChineseNumb));
                    //条文ID lijx 进行对该方法进行添加一个参数，表示之几
                    string provID = QuoteLawExt.GetProvID(CatID, LawLibraryCode, provNumb, provNumbzhi);
                    if (provChineseNumb.Trim().Equals(provNumb.ToString()))
                    { //2018-03-12 ljx 这里的判断是用来表示如果法条内容是数字，需要进行转大写
                        string daxie = LdFormat.CmycurD(provNumb);
                        provName = Regex.Replace(provName, provNumb.ToString(), daxie);
                    }
                    if (provID == string.Empty || provChineseNumb == string.Empty)
                    {
                        continue;
                    }
                    string quoteProvID = string.Empty;
                    LawProv lawProv = AddLawPro(provID, provName, lawModel);
                    //添加款的项
                    string MReg = RuleParseClass.GerRegValue("QuoteLawExtractA6");
                    tempMatch = Regex.Match(singleQuoteprov, MReg);
                    string clauseName = (tempMatch == null) ? "" : tempMatch.Value;
                    string ClauseChineseNumb = (tempMatch == null) ? "" : tempMatch.Groups[1].Value;
                    int ClauseNumb = Convert.ToInt32(LdFormat.DecodeNumberCn(ClauseChineseNumb));    //款序号
                    if (ClauseChineseNumb.Trim().Equals(ClauseNumb.ToString()))
                    { //2018-03-12 ljx 这里的判断是用来表示款内容是数字，需要进行转大写
                        string daxie = LdFormat.CmycurD(ClauseNumb);
                        clauseName = Regex.Replace(clauseName, ClauseNumb.ToString(), daxie);
                    }
                    LawClause lawClause = new LawClause();
                    string quoteclauseID = string.Empty;
                    if (ClauseChineseNumb != string.Empty)
                    {
                        //目前用的款ID，即款的序号。
                        string clauseID = ClauseNumb.ToString();     // this.GetClauseID(CatID, provID, ClauseNumb, 0, true);                 //款ID
                        if (clauseID != string.Empty)
                        {
                            lawClause = AddLawClause(clauseID, clauseName, lawProv);
                        }
                    }
                    //添加项
                    string TMReg = RuleParseClass.GerRegValue("QuoteLawExtractA8");
                    tempMatch = Regex.Match(singleQuoteprov, TMReg);
                    string ItemName = (tempMatch == null) ? "" : tempMatch.Value;
                    string ItemChineseNumb = (tempMatch == null) ? "" : tempMatch.Groups[1].Value;
                    int ItemNumb = Convert.ToInt32(LdFormat.DecodeNumberCn(ItemChineseNumb));        //项序号
                    if (ItemChineseNumb.Trim().Equals(ItemNumb.ToString()))
                    { //2018-03-12 ljx 这里的判断是用来表示项内容是数字，需要进行转大写
                        string daxie = LdFormat.CmycurD(ItemNumb);
                        ItemName = Regex.Replace(ItemName, ItemNumb.ToString(), daxie);
                    }
                    if (ItemChineseNumb != string.Empty)
                    {
                        //目前用的项ID，即项的序号。
                        string itemID = ItemNumb.ToString();
                        AddLawItem(itemID, ItemName, lawClause);
                    }
                }
            }

            // 合并节点
            //MergerLawNodes();
        }

        /// <summary>
        /// 解析配置信息
        /// </summary>
        private void ParseConfig()
        {
            string configcontent = dataContext.DataLayerSys.GetConfigModel(ModuleName).ConfigContent;
            courtTrailQuotLawConfig = XmlUtility.DeserializeToObject<CircumsatanModel>(configcontent);
        }
    }
}
